/*
 * Copyright 2019-2021 shoprh.com. All rights reserved.
 * Support: http://www.shoprh.com
 * License: http://www.shoprh.com/license
 */
package com.shoprh.shop.controller;

import cn.hutool.core.util.EnumUtil;
import com.shoprh.shop.domain.Ad;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.shoprh.common.core.domain.R;
import com.shoprh.common.core.controller.BaseController;
import com.shoprh.shop.service.IAdService;
import org.wf.jwtp.annotation.RequiresPermissions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller - 广告
 *
 * @author 若狐 Team
 * @version 1.0
 */
@RestController
@RequestMapping("admin/ad")
public class AdController extends BaseController {

    @Autowired
    private IAdService adService;

    @RequiresPermissions("admin:ad:list")
    @GetMapping("type")
    public R type() {
        List<Map<String, Object>> mapList = new ArrayList<>();
        Map<String, Ad.Type>      enumMap = EnumUtil.getEnumMap(Ad.Type.class);
        for (Map.Entry<String, Ad.Type> entry : enumMap.entrySet()) {
            Map<String, Object> map = new HashMap<>();
            map.put("value", entry.getKey());
            map.put("text", entry.getValue().getName());
            mapList.add(map);
        }
        return R.data(mapList);
    }

    /**
     * 查询广告列表
     */
    @RequiresPermissions("admin:ad:list")
    @GetMapping("list")
    public R list(Ad ad) {
        startPage();
        return result(adService.selectAdList(ad));
    }

    /**
     * 新增保存广告
     */
    @RequiresPermissions("admin:ad:add")
    @PostMapping("save")
    public R addSave(@RequestBody Ad ad) {
        return toAjax(adService.insertAd(ad));
    }

    /**
     * 修改保存广告
     */
    @RequiresPermissions("admin:ad:edit")
    @PostMapping("update")
    public R editSave(@RequestBody Ad ad) {
        return toAjax(adService.updateAd(ad));
    }

    /**
     * 删除广告
     */
    @RequiresPermissions("admin:ad:remove")
    @PostMapping("remove")
    public R remove(String ids) {
        return toAjax(adService.deleteAdByIds(ids));
    }

}
